<?
//Symbiont Ajax 0.1.9
class sym_ajax extends symbiont{
	function ___main($design=""){
		global $kernel, $sym, $user;
		if(isset($_GET['upload'])){
			if($user->access_level>=9){
				$this->upload();
			}
		}
		elseif(isset($_POST['file'])){
			$design=data::file_system($_POST['file']);
		}
		elseif(isset($_GET['file'])){
			$design=data::file_system($_GET['file']);
		}
		elseif(isset($_POST['symbiont'])){
			$symbiont=data::symbiont($_POST['symbiont']);
		}
		elseif(isset($_GET['symbiont'])){
			$symbiont=data::symbiont($_GET['symbiont']);
		}
		elseif(isset($_POST['administrate'])){
			global $kernel, $sym;
			$administrate=design::symbiont_explode($_POST['administrate']);
			$kernel->symbiont_add($administrate['symbiont']);
			$sym->$administrate['symbiont']->_administrate($administrate);
		}
		elseif(isset($_POST['resize'])&&isset($_POST['width'])&&isset($_POST['postfix'])){
			global $kernel;
			$kernel->library_add('image');
			$url=data::file_system($_POST['resize']);
			if(!data::is_uint($_POST['width'])) return;
			$width=$_POST['width'];
			$postfix=data::word($_POST['postfix']);
			$img=new image($url);
			$name=$img->resize_fixed_width($postfix, $width);
			print('{"ok":"'.$name.'"}');
		}
		elseif(isset($_POST['label'])){
			$label=data::word($_POST['label'], 'en', 0, '\.');
			$l=new label($label);
			return $l->get();
		}
		if(isset($design)&&$design) $d=new design($design, "ajax");
		if(isset($symbiont)&&$symbiont){
			global $sym, $kernel;
			
			$s=explode('|', $symbiont);
			$x=design::symbiont($s[0], isset($s[1])?$s[1]:'');
			eval($x[1]);
			$kernel->inc();
			eval("print ".$x[0].";");
		}
	}
	//File uploader
	function upload(){
		global $kernel;
		$error = "";
		$msg = "";
		$fileElementName = data::word($_GET['name'], "en");
		$new_name=isset($_GET['new_name'])?data::file_name($_GET['new_name']):'';
		$dir=data::file_system($_GET['dir']);
		if(!empty($_FILES[$fileElementName]['error'])){
			switch($_FILES[$fileElementName]['error']){
				case '1':
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2':
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3':
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$error = 'No file was uploaded.';
					break;
				case '6':
					$error = 'Missing a temporary folder';
					break;
				case '7':
					$error = 'Failed to write file to disk';
					break;
				case '8':
					$error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$error = 'No error code avaiable';
			}
		}
		elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none'){
			$error = 'No file was uploaded..';
		}
		else{
			$name=data::file_name($_FILES[$fileElementName]['name']);
			$size=@filesize($_FILES[$fileElementName]['tmp_name']);
			$type=data::file_type($_FILES[$fileElementName]['name']);
			if($new_name==''){
				$new_name=data::file_name_without_type($_FILES[$fileElementName]['name']);
			}
			$right_name=$new_name;
			if(file_exists($dir.$right_name.".".$type)){
				$i=1;
				while(file_exists($dir.$right_name."_".$i.".".$type)){
					$i++;
				}
				$right_name.="_".$i;
			}
			if(!@move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $dir.$right_name.".".$type)){
				$error="Can't save file";
			}
		}		
		echo "{";
		echo "error: '".$error."',\n";
		echo "name: '".$right_name.".".$type."',\n";
		echo "size: '".$size."',\n";
		echo "url: '".$kernel->site->path.$dir.$right_name.".".$type."'";
		echo "}";
	}
	public function upload_status(){
		$status = uploadprogress_get_info($_POST['file']); 
		echo json_encode($status);
	}
}
?>